#!/usr/bin/python

# flashcardgui.py
# this is a PyQt gui for the greek flashcard program

from PyQt4 import QtGui, QtCore
from flashcard import FlashCard
from vocab import Vocab
from constants import flashblack_version

class FlashCardGui(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        
        self.everythingLayout = QtGui.QVBoxLayout(self)
        self.vocab = Vocab(self)
        
        self.setGeometry(100, 100, 500, 300)
        self.setWindowTitle('Flashblack - Language Flash Card Application')
        self.menuBar = QtGui.QMenuBar(self)
        self.menuBar.setMinimumWidth(200)
        
        self.vocabMenu = self.menuBar.addMenu('Vocab')
        self.newVocabAction = QtGui.QAction(self)
        self.newVocabAction.setText('New')
        self.connect(self.newVocabAction, QtCore.SIGNAL('triggered()'), self.vocab.newVocab)
        self.vocabMenu.addAction(self.newVocabAction)
        self.loadVocabAction = QtGui.QAction(self)
        self.loadVocabAction.setText('Open...')
        self.connect(self.loadVocabAction, QtCore.SIGNAL('triggered()'), self.vocab.loadVocab)
        self.vocabMenu.addAction(self.loadVocabAction)
        self.saveVocabAction = QtGui.QAction(self)
        self.saveVocabAction.setText('Save')
        self.saveVocabAction.setEnabled(False)
        self.connect(self.saveVocabAction, QtCore.SIGNAL('triggered()'), self.vocab.saveVocab)
        self.vocabMenu.addAction(self.saveVocabAction)
        self.saveVocabAsAction = QtGui.QAction(self)
        self.saveVocabAsAction.setText('Save As...')
        self.saveVocabAsAction.setEnabled(False)
        self.connect(self.saveVocabAsAction, QtCore.SIGNAL('triggered()'), self.vocab.saveVocabAs)
        self.vocabMenu.addAction(self.saveVocabAsAction)
        self.editWordTypesAction = QtGui.QAction(self)
        self.editWordTypesAction.setText('Edit Word Types...')
        self.editWordTypesAction.setEnabled(False)
        self.connect(self.editWordTypesAction, QtCore.SIGNAL('triggered()'), self.vocab.editWordTypes)
        self.vocabMenu.addAction(self.editWordTypesAction)
        self.editFontsAction = QtGui.QAction(self)
        self.editFontsAction.setText('Edit Fonts...')
        self.editFontsAction.setEnabled(False)
        self.connect(self.editFontsAction, QtCore.SIGNAL('triggered()'), self.vocab.editFonts)
        self.vocabMenu.addAction(self.editFontsAction)
        self.editDifficultyAction = QtGui.QAction(self)
        self.editDifficultyAction.setText('Edit Change in Difficulty...')
        self.editDifficultyAction.setEnabled(False)
        self.connect(self.editDifficultyAction, QtCore.SIGNAL('triggered()'), self.vocab.editDifficultyChange)
        self.vocabMenu.addAction(self.editDifficultyAction)
        
        self.cardsMenu = self.menuBar.addMenu('Card')
        self.cardsMenu.setEnabled(False)
        self.editCurrentAction = QtGui.QAction(self)
        self.editCurrentAction.setText('Edit...')
        self.editCurrentAction.setEnabled(False)
        self.connect(self.editCurrentAction, QtCore.SIGNAL('triggered()'), self.vocab.editCurrentCard)
        self.cardsMenu.addAction(self.editCurrentAction)
        self.dupCurrentAction = QtGui.QAction(self)
        self.dupCurrentAction.setText('Duplicate')
        self.dupCurrentAction.setEnabled(False)
        self.connect(self.dupCurrentAction, QtCore.SIGNAL('triggered()'), self.vocab.duplicateCurrentCard)
        self.cardsMenu.addAction(self.dupCurrentAction)
        self.newCardAction = QtGui.QAction(self)
        self.newCardAction.setText('New...')
        self.connect(self.newCardAction, QtCore.SIGNAL('triggered()'), self.vocab.newCard)
        self.cardsMenu.addAction(self.newCardAction)
        self.deleteCardAction = QtGui.QAction(self)
        self.deleteCardAction.setText('Delete')
        self.deleteCardAction.setEnabled(False)
        self.connect(self.deleteCardAction, QtCore.SIGNAL('triggered()'), self.vocab.deleteCurrentCard)
        self.cardsMenu.addAction(self.deleteCardAction)
        
        self.toolsMenu = self.menuBar.addMenu('Tools')
        self.toolsMenu.setEnabled(False)
        self.selectActiveAction = QtGui.QAction(self)
        self.selectActiveAction.setText('Select Active Cards...')
        self.connect(self.selectActiveAction, QtCore.SIGNAL('triggered()'), self.vocab.selectActiveCards)
        self.toolsMenu.addAction(self.selectActiveAction)
        self.shuffleActiveAction = QtGui.QAction(self)
        self.shuffleActiveAction.setText('Shuffle Active Cards')
        self.shuffleActiveAction.setEnabled(False)
        self.connect(self.shuffleActiveAction, QtCore.SIGNAL('triggered()'), self.vocab.shuffleActiveCards)
        self.toolsMenu.addAction(self.shuffleActiveAction)
        
        self.helpMenu = self.menuBar.addMenu('Help')
        self.aboutAction = QtGui.QAction(self)
        self.aboutAction.setText('About')
        self.connect(self.aboutAction, QtCore.SIGNAL('triggered()'), self.about)
        self.helpMenu.addAction(self.aboutAction)
        
        self.everythingLayout.addStretch()
        self.everythingLayout.addWidget(self.vocab.activeCards)
        self.everythingLayout.addWidget(self.vocab.progress)
        self.everythingLayout.addStretch()
        
        self.buttonPanel = QtGui.QHBoxLayout()
        self.previousButton = QtGui.QPushButton('Previous', self)
        self.revealButton = QtGui.QPushButton('Reveal', self)
        self.wrongButton = QtGui.QPushButton('I got it wrong', self)
        self.rightButton = QtGui.QPushButton('I got it right', self)
        self.hintButton = QtGui.QPushButton('Hint', self)
        self.nextButton = QtGui.QPushButton('Next', self)
        self.buttonPanel.addWidget(self.previousButton)
        self.buttonPanel.addWidget(self.revealButton)
        self.buttonPanel.addWidget(self.wrongButton)
        self.buttonPanel.addWidget(self.rightButton)
        self.buttonPanel.addWidget(self.hintButton)
        self.buttonPanel.addWidget(self.nextButton)
        self.setButtonsEnabled(False)
        self.everythingLayout.addLayout(self.buttonPanel)
        
        self.connect(self.revealButton, QtCore.SIGNAL('clicked()'), self.vocab.revealCurrentCard)
        self.connect(self.hintButton, QtCore.SIGNAL('clicked()'), self.vocab.revealCurrentCardHint)
        self.connect(self.nextButton, QtCore.SIGNAL('clicked()'), self.vocab.nextCard)
        self.connect(self.previousButton, QtCore.SIGNAL('clicked()'), self.vocab.previousCard)
        self.connect(self.wrongButton, QtCore.SIGNAL('clicked()'), self.vocab.gotItWrong)
        self.connect(self.rightButton, QtCore.SIGNAL('clicked()'), self.vocab.gotItRight)
    
    def about(self):
        QtGui.QMessageBox.about(self, 'About Flashblack', flashblack_version + "\nhttp://code.google.com/p/flashblack/")
    
    def keyPressEvent(self, event):
        key = event.key()
        if key == QtCore.Qt.Key_Comma and self.previousButton.isEnabled():
            self.vocab.previousCard()
        if key == QtCore.Qt.Key_Period and self.nextButton.isEnabled():
            self.vocab.nextCard()
        if key == QtCore.Qt.Key_Minus and self.wrongButton.isEnabled():
            self.vocab.gotItWrong()
        if (key == QtCore.Qt.Key_Plus or key == QtCore.Qt.Key_Equal) and self.rightButton.isEnabled():
            self.vocab.gotItRight()
        if key == QtCore.Qt.Key_0 and self.revealButton.isEnabled():
            self.vocab.revealCurrentCard()
        else:
            QtGui.QWidget.keyPressEvent(self, event)
            

    def closeEvent(self, event):
        if self.vocab.changed:
            reply = QtGui.QMessageBox.warning(self, 'Vocab has been changed',
                "Would you like to save your changes to the vocab?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel)
            if reply == QtGui.QMessageBox.Yes:
                self.vocab.saveVocab()
                event.accept()
            elif reply == QtGui.QMessageBox.No:
                event.accept()
            else:
                event.ignore()

    def setButtonsEnabled(self, enable = True):
        self.revealButton.setEnabled(enable)
        self.hintButton.setEnabled(enable)
        self.nextButton.setEnabled(enable)
        self.previousButton.setEnabled(enable)
        self.wrongButton.setEnabled(enable)
        self.rightButton.setEnabled(enable)
    
    def setVocabActionsEnabled(self, enable = True):
        self.saveVocabAction.setEnabled(enable)
        self.saveVocabAsAction.setEnabled(enable)
        self.editWordTypesAction.setEnabled(enable)
        self.editFontsAction.setEnabled(enable)
        self.editDifficultyAction.setEnabled(enable)
        self.cardsMenu.setEnabled(enable)
        self.toolsMenu.setEnabled(enable)
    
    def setCardActionsEnabled(self, enable = True):
        self.shuffleActiveAction.setEnabled(enable)
        self.editCurrentAction.setEnabled(enable)
        self.dupCurrentAction.setEnabled(enable)
        self.deleteCardAction.setEnabled(enable)

